{% extends "layout.html" %}
{% block __data_browser__custom_external_block %}
  <script type="text/javascript">
    $(function () {
      $("#department_id").change(function () {
        update_procedure();
      });
      var departments = {{ department_list|tojson|safe }};

      function update_procedure() {
        $("#procedure_id").children("option[value!='']").remove();
        var selected_department_id = $("#department_id").val();
        for (i = 0; i < departments.length; ++i) {
          if (departments[i].id == selected_department_id) {
            var procedure_list = departments[i].procedure_list;
            for (j = 0; j < procedure_list.length; ++j) {
              $("#procedure_id").append("<option value=" + procedure_list[j].id + ">" + procedure_list[j].name + "</option>");
            }
          }
        }
        $("#procedure_id").select2({
          width: "element",
          placeholder: "请选择",
          matcher: function (term, text) {
            return pinyin.getCamelChars(text).toUpperCase().indexOf(term.toUpperCase()) >= 0;
          }
        });
      }

      update_procedure();
    })

  </script>
{% endblock %}
{% block body %}
  <div class="container">
    <legend>排产</legend>
    <form class="form-horizontal" method="POST">
      <div class="form-group">
        <label class="control-label col-lg-2">已选择的工单号 </label>

        <div class="col-lg-3">
          <p class="form-control-static">
            {{ work_command_list|join(", ",attribute="id") }}
          </p>
        </div>
      </div>
      <div class="form-group">
        <label class="control-label col-lg-2">数量(单位:KG) </label>

        <div class="col-lg-3">
          <p class="form-control-static">
            {{ work_command_list|sum(attribute="org_weight") }}
          </p>
        </div>
      </div>
      <div class="form-group">
        <label for="department_id" class="control-label col-lg-2">车间
        </label>

        <div class="col-lg-3">
          <select size="1" name="department_id" id="department_id" class="form-control" required='required'>
            {% for department in department_list %}
              <option value="{{ department.id }}"
                  {% if default_department_id and department.id == default_department_id %}
                      selected="selected"
                  {% endif %} >{{ department.name }}</option>
            {% endfor %}
          </select>
          <span class="help-block">
              {% for grouper, list in work_command_list|groupby("default_department") %}
                工单<em>{{ list|join(",", attribute="id") }}</em>的默认车间是<em>{{ grouper }}</em><br>
              {% endfor %}
          </span>
        </div>
      </div>
      <div class="form-group">
        <label for="tech_req" class="control-label col-lg-2">技术要求</label>

        <div class="col-lg-3">
          <input size="10" name="tech_req" id="tech_req" type="text" class="form-control"/>
        </div>
      </div>
      <div class="form-group">
        <label for="urgent-input" class="control-label col-lg-2">加急</label>

        <div class="col-lg-3">
          <div class="checkbox">
            <input name="urgent" id="urgent-input" type="checkbox"/>
          </div>
        </div>
      </div>
      <div class="form-group">
        <label class="control-label col-lg-2" for="procedure_id">工序</label>

        <div class="col-lg-3">
          <select size="1" name="procedure_id" id="procedure_id" class="form-control">
            <option selected="selected" disabled="disabled" value="">
              保持现有工序
            </option>
          </select>
           <span class="help-block">
             {% for grouper, list in work_command_list|groupby("procedure") %}
               {% if grouper is none %}
                 工单<em>{{ list|join("," ,attribute="id") }}</em>无工序<br>
                 <span class="text-danger">请选择工序</span>
                 <script>$(function(){
                   $("#procedure_id").val(1).attr("required", true);
                 })</script>
               {% else %}
                 工单<em>{{ list|join("," ,attribute="id") }}</em>的工序是<em>{{ grouper or "" }}</em><br>
               {% endif %}
             {% endfor %}
           </span>
        </div>
      </div>
      <input type="hidden" name="url" value="{{ request.args.get("url") or url_for("manufacture.work_command_list") }}"/>

      <div class="form-group">
        <div class="col-lg-3 col-lg-offset-2">
          <button class="btn btn-primary btn-lg">
            <i class="fa fa-wrench fa-fw"></i>排产
          </button>
          <a class="btn btn-lg btn-default" href="{{ request.args.get("url") or url_for("manufacture.work_command_list") }}">
            <i class="fa fa-step-backward fa-fw"></i>返回
          </a>
        </div>
      </div>
    </form>
  </div>

{% endblock %}
